﻿/*
 * Created by SharpDevelop.
 * User: lin
 * Date: 2019/4/14
 * Time: 14:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using SuperWeight;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using Modbus;
using SuperSocket;
using SuperSocket.SocketEngine;
using SuperSocket.SocketService;



namespace supersocketest1
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			CheckForIllegalCrossThreadCalls = false;
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		//--------------------------------------------------------
		private WeightHelper weight01;
		void Btnblance1openClick(object sender, EventArgs e)
		{
			if (weight01 == null)
            {
                weight01 = new WeightHelper();
                weight01.WeightReceive += Weight01_WeightReceive;
                //weight01.WeightReceive += Weight01_WeightReceive;
            }
            if (weight01.Open(this.texblanceIP1.Text, int.Parse(this.texblancePort1.Text)))
            {
                //MessageBox.Show("1号称连接成功");
                this.btnblance1open.BackColor = Color.Red;
                this.richTextBox1.AppendText(this.texblanceIP1.Text+"--1号称连接成功--"+DateTime.Now.ToString()+"\n");
            }
	
		}
		
		void Btnblance1closeClick(object sender, EventArgs e)
		{
			
		 	if (weight01 != null)
            {
                if (weight01.Close())
                {
                    //MessageBox.Show("1号称关闭成功");
                    this.btnblance1open.BackColor = Color.Gray;
                    this.lblblancesign1.Text = "";
                    this.lblblancenum1.Text = "";
                    this.lblblancekg1.Text = "";
                    this.richTextBox1.AppendText(this.texblanceIP1.Text+"--1号称关闭成功--"+DateTime.Now.ToString()+"\n");
                }
            }
	
		}
		
		
		
		 private void Weight01_WeightReceive(WeightPackageInfo Data)
        {
            //内容头
            var Header = Data.Header;
            //正或负
            var PlusOrMinux = Data.PlusOrMinux;
            //重量
            var Weight = Data.Weight;
            //单位
            var UnitSysbol = Data.UnitSysbol;

            //读取到的原字符串
            var SourceStr = Data.Body;
            //读取到的原Byte字节
            var SourceBytes = Data.Byte_Body;
            //this.richTextBox1.AppendText(Data.Byte_Body.ToString());

            this.texblance1.Text = string.Format("{0}{1}{2}{3}", Data.Header, Data.PlusOrMinux, Data.Weight, Data.UnitSysbol);
            this.lblblancesign1.Text = Data.PlusOrMinux.ToString();
            this.lblblancenum1.Text = Data.Weight.ToString();
            this.lblblancekg1.Text = Data.UnitSysbol.ToString();
        }
		 
		 //--------------------------------------------------------
		 private WeightHelper weight02;
		void Btnblance2openClick(object sender, EventArgs e)
		{
			if (weight02 == null)
            {
                weight02 = new WeightHelper();
                weight02.WeightReceive += Weight02_WeightReceive;
                //weight01.WeightReceive += Weight01_WeightReceive;
            }
            if (weight02.Open(this.texblanceIP2.Text, int.Parse(this.texblancePort2.Text)))
            {
                //MessageBox.Show("2号称连接成功");
                this.btnblance2open.BackColor = Color.Red;
                this.richTextBox1.AppendText(this.texblanceIP2.Text+"--2号称连接成功--"+DateTime.Now.ToString()+"\n");
            }
	
		}
		void Btnblance2closeClick(object sender, EventArgs e)
		{
			
			if (weight02 != null)
            {
                if (weight02.Close())
                {
                    //MessageBox.Show("2号称关闭成功");
                    this.btnblance2open.BackColor = Color.Gray;
                    this.lblblancesign2.Text = "";
                    this.lblblancenum2.Text = "";
                    this.lblblancekg2.Text = "";
                    this.richTextBox1.AppendText(this.texblanceIP2.Text+"--2号称关闭成功--"+DateTime.Now.ToString()+"\n");
                }
            }
	
		}
		private void Weight02_WeightReceive(WeightPackageInfo Data)
        {
            //内容头
            var Header = Data.Header;
            //正或负
            var PlusOrMinux = Data.PlusOrMinux;
            //重量
            var Weight = Data.Weight;
            //单位
            var UnitSysbol = Data.UnitSysbol;

            //读取到的原字符串
            var SourceStr = Data.Body;
            //读取到的原Byte字节
            var SourceBytes = Data.Byte_Body;

            //this.texblance2.Text = string.Format("状态{0},符号{1},重量{2},单位{3}", Data.Header, Data.PlusOrMinux, Data.Weight, Data.UnitSysbol);
            this.texblance2.Text = string.Format("{0}{1}{2}{3}", Data.Header, Data.PlusOrMinux, Data.Weight, Data.UnitSysbol);
            this.lblblancesign2.Text = Data.PlusOrMinux;
            this.lblblancenum2.Text = Data.Weight;
            this.lblblancekg2.Text = Data.UnitSysbol;
        }
		
		//--------------------------------------------------------
		private WeightHelper weight03;
		void Btnblance3openClick(object sender, EventArgs e)
		{
			if (weight03 == null)
            {
                weight03 = new WeightHelper();
                weight03.WeightReceive += Weight03_WeightReceive;
                
            }
            if (weight03.Open(this.texblanceIP3.Text, int.Parse(this.texblancePort3.Text)))
            {
                //MessageBox.Show("3号称连接成功");
                this.btnblance3open.BackColor = Color.Red;
                this.richTextBox1.AppendText(this.texblanceIP3.Text+"--3号称连接成功--"+DateTime.Now.ToString()+"\n");
            }
	
		}
		void Btnblance3closeClick(object sender, EventArgs e)
		{
			if (weight03 != null)
            {
                if (weight03.Close())
                {
                    //MessageBox.Show("3号称关闭成功");
                    this.btnblance3open.BackColor = Color.Gray;
                    this.lblblancesign3.Text = "";
                    this.lblblancenum3.Text = "";
                    this.lblblancekg3.Text = "";
                    this.richTextBox1.AppendText(this.texblanceIP3.Text+"--3号称关闭成功--"+DateTime.Now.ToString()+"\n");
                }
            }
	
		}
		private void Weight03_WeightReceive(WeightPackageInfo Data)
        {
            //内容头
            var Header = Data.Header;
            //正或负
            var PlusOrMinux = Data.PlusOrMinux;
            //重量
            var Weight = Data.Weight;
            //单位
            var UnitSysbol = Data.UnitSysbol;

            //读取到的原字符串
            var SourceStr = Data.Body;
            //读取到的原Byte字节
            var SourceBytes = Data.Byte_Body;

            this.texblance3.Text = string.Format("{0}{1}{2}{3}", Data.Header, Data.PlusOrMinux, Data.Weight, Data.UnitSysbol);
            this.lblblancesign3.Text = Data.PlusOrMinux;
            this.lblblancenum3.Text = Data.Weight;
            this.lblblancekg3.Text = Data.UnitSysbol;
        }
		
		//--------------------------------------------------------
		private WeightHelper weight04;
		void Btnblance4openClick(object sender, EventArgs e)
		{
			if (weight04 == null)
            {
                weight04 = new WeightHelper();
                weight04.WeightReceive += Weight04_WeightReceive;
                
            }
            if (weight04.Open(this.texblanceIP4.Text, int.Parse(this.texblancePort4.Text)))
            {
                //MessageBox.Show("4号称连接成功");
                this.btnblance4open.BackColor = Color.Red;
                this.richTextBox1.AppendText(this.texblanceIP4.Text+"--4号称连接成功--"+DateTime.Now.ToString()+"\n");
            }
	
		}
		void Btnblance4closeClick(object sender, EventArgs e)
		{
			 
			if (weight04 != null)
            {
                if (weight04.Close())
                {
                    //MessageBox.Show("4号称关闭成功");
                    this.btnblance4open.BackColor = Color.Gray;
                    this.lblblancesign4.Text = "";
                    this.lblblancenum4.Text = "";
                    this.lblblancekg4.Text = "";
                    this.richTextBox1.AppendText(this.texblanceIP4.Text+"--4号称关闭成功--"+DateTime.Now.ToString()+"\n");
                }
            }
	
		}
		private void Weight04_WeightReceive(WeightPackageInfo Data)
        {
            //内容头
            var Header = Data.Header;
            //正或负
            var PlusOrMinux = Data.PlusOrMinux;
            //重量
            var Weight = Data.Weight;
            //单位
            var UnitSysbol = Data.UnitSysbol;

            //读取到的原字符串
            var SourceStr = Data.Body;
            //读取到的原Byte字节
            var SourceBytes = Data.Byte_Body;

            this.texblance4.Text = string.Format("{0}{1}{2}{3}", Data.Header, Data.PlusOrMinux, Data.Weight, Data.UnitSysbol);
            this.lblblancesign4.Text = Data.PlusOrMinux;
            this.lblblancenum4.Text = Data.Weight;
            this.lblblancekg4.Text = Data.UnitSysbol;
        }
		
		//--------------------------------------------------------
		private WeightHelper weight05;
		void Btnblance5openClick(object sender, EventArgs e)
		{
			
			if (weight05 == null)
            {
                weight05 = new WeightHelper();
                weight05.WeightReceive += Weight05_WeightReceive;
                
            }
            if (weight05.Open(this.texblanceIP5.Text, int.Parse(this.texblancePort5.Text)))
            {
                //MessageBox.Show("5号称连接成功");
                this.btnblance5open.BackColor = Color.Red;
                this.richTextBox1.AppendText(this.texblanceIP5.Text+"--5号称连接成功--"+DateTime.Now.ToString()+"\n");
            }
	
		}
		void Btnblance5closeClick(object sender, EventArgs e)
		{
			if (weight05 != null)
            {
                if (weight05.Close())
                {
                    //MessageBox.Show("5号称关闭成功");
                    this.btnblance5open.BackColor = Color.Gray;
                    this.lblblancesign5.Text = "";
                    this.lblblancenum5.Text = "";
                    this.lblblancekg5.Text = "";
                    this.richTextBox1.AppendText(this.texblanceIP5.Text+"--5号称关闭成功--"+DateTime.Now.ToString()+"\n");
                }
            }
	
		}
		private void Weight05_WeightReceive(WeightPackageInfo Data)
        {
            //内容头
            var Header = Data.Header;
            //正或负
            var PlusOrMinux = Data.PlusOrMinux;
            //重量
            var Weight = Data.Weight;
            //单位
            var UnitSysbol = Data.UnitSysbol;

            //读取到的原字符串
            var SourceStr = Data.Body;
            //读取到的原Byte字节
            var SourceBytes = Data.Byte_Body;

            this.texblance5.Text = string.Format("{0}{1}{2}{3}", Data.Header, Data.PlusOrMinux, Data.Weight, Data.UnitSysbol);
            this.lblblancesign5.Text = Data.PlusOrMinux;
            this.lblblancenum5.Text = Data.Weight;
            this.lblblancekg5.Text = Data.UnitSysbol;
        }
		
		TcpClient client ;
		void BtnTestopenClick(object sender, EventArgs e)
		{
			
			client = new TcpClient();
			if (client.Connected==false) {
				client.Connect(IPAddress.Parse(this.tbxtest1IP.Text),int.Parse(this.tbx2testPORT.Text));
			}
			
            if (client.Connected) {
            	MessageBox.Show("测试连接读取数据确认");
            	this.richTextBox1.AppendText(this.tbxtest1IP.Text+"测试连接成功----->"+DateTime.Now.ToString()+"\n");
            	
            }
            
            NetworkStream nstcp = client.GetStream();
            StreamReader SR = new StreamReader(nstcp);
            int deatalen =client.Available;
            byte[] receivedata=new byte[2048];
            
            nstcp.Read(receivedata,0,deatalen);
            string rcdata =ASCIIEncoding.ASCII.GetString(receivedata).Substring(0,deatalen);
           
            this.richTextBox1.AppendText(rcdata);
            if (rcdata.Length>=200) {
            	rcdata="";
            }
            
            //client.Close();
                      
	
		}
		void BtntestcloseClick(object sender, EventArgs e)
		{
			if (client!=null && client.Connected) {
				client.Close();
				this.richTextBox1.AppendText(this.tbxtest1IP.Text+"测试连接关闭成功----->"+DateTime.Now.ToString()+"\n");
			}
			
	
		}
		void MainFormFormClosed(object sender, FormClosedEventArgs e)
		{
			if (client!=null && client.Connected) {
				client.Close();
			}
			if (weight01 != null)
            {
                if (weight01.Close())
                {
                    //MessageBox.Show("1号称关闭成功");
                    
                }
            }
			if (weight02 != null)
            {
                if (weight02.Close())
                {
                    //MessageBox.Show("2号称关闭成功");
                    
                }
            }
			if (weight03 != null)
            {
                if (weight03.Close())
                {
                    //MessageBox.Show("3号称关闭成功");
                    
                }
            }
			if (weight04 != null)
            {
                if (weight04.Close())
                {
                    //MessageBox.Show("4号称关闭成功");
                    
                }
            }
			if (weight05 != null)
            {
                if (weight05.Close())
                {
                    //MessageBox.Show("5号称关闭成功");
                    
                }
            }
			
	
		}
		void BtnServiceClick(object sender, EventArgs e)
		{
			FormServer formServ=new FormServer();
			formServ.Show();
	
		}
		void BtnclientClick(object sender, EventArgs e)
		{
			FormClient formCli=new FormClient();
			formCli.Show();
	
		}
		
		
	}
}
